﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Text;
using MyLib;
 

namespace PerformanceReport
{
    public partial class ProcessAvg : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                //BindData();               
                Page.ClientScript.RegisterStartupScript(Page.GetType(), "onload", "document.getElementById('container').style.display='none'", true); 
            }
        }

        private void BindData()
        {
            try
            {
                DataTable dt;
                int PageIndex = AspNetPager1.CurrentPageIndex - 1;
                int PageSize = AspNetPager1.PageSize;

                //string strSql = "Select  ProcessVersion,ProcessName,starttime,endtime,INCIDENT, "
                //            + "ROUND(TO_NUMBER(to_date(endtime) - to_date(starttime)) * 24) as Hour"
                //            + "  from tasks";
                string strSql = " Select max(ProcessVersion) as Ver ,ProcessName, "         //得到最大的版本号（即最新版本号）
                             // + "ROUND(avg(nvl(ROUND(TO_NUMBER(to_date(endtime) - to_date(starttime)))* 24 ,0)),2) as Hours"   //平均消耗（小时）,并且保留两位有效数字
                              +"ROUND(avg(ROUND(to_date(endtime) - to_date(starttime)))* 24 ,2) as Hours  "
                              + "  from Incidents";
                string where = " where 1=1 ";
                where += " and ProcessName like N'" + drpBusinessUnit.SelectedValue.Trim() + "%'";
                if (!string.IsNullOrEmpty(txtStart.Text))
                {
                    where += " and  starttime >= to_date('" + txtStart.Text.Trim() + "','yyyy-MM-dd')";
                }
                if (!string.IsNullOrEmpty(txtEnd.Text))
                {
                    where += " and  starttime <= to_date('" + txtEnd.Text.Trim() + "','yyyy-MM-dd')";
                }
                where += "  group by ProcessName  order by ProcessName";
                string StrSqlTotal = strSql + where;
                dt = DataAccess.Instance("BizDB").ExecuteDataTable(StrSqlTotal.ToString());

                //初始化分页数据源实例
                PagedDataSource pds = new PagedDataSource();

                //设置总行数
                AspNetPager1.RecordCount = dt.Rows.Count;
                //设置分页的数据源
                pds.DataSource = dt.DefaultView;
                //设置当前页
                pds.CurrentPageIndex = AspNetPager1.CurrentPageIndex - 1;
                //设置每页显示页数
                pds.PageSize = AspNetPager1.PageSize;
                //启用分页
                pds.AllowPaging = true;

                //设置Repeater的数据源为分页数据源
                rpSource.DataSource = pds;
                rpSource.DataBind();
                lbTotal.Text = "共" + dt.Rows.Count.ToString() + "条记录";

            }
            catch (Exception ex)
            {
                MessageBox("alert('" + ex.Message + "');");
            }
        }

        protected void AspNetPager1_PageChanged(object sender, EventArgs e)
        {
            //AspNetPager1.CurrentPageIndex = e.;

            BindData();
            Page.ClientScript.RegisterStartupScript(Page.GetType(), "onload", "document.getElementById('container').style.display='none'", true); 

        }

        ////平均消耗（天）
        //public string GetAvgDay(string strHour)
        //{
        //    string strAvgDay = null;
        //    if (strHour != "")
        //    {
        //        double dDay = double.Parse(strHour);
        //        double dAvgDay = Math.Round(dDay/24, 0, MidpointRounding.AwayFromZero);
        //        strAvgDay = dAvgDay.ToString();
        //    }
        //    return strAvgDay;
            
        //}

        public string GetAvgDay(string strHour)
        {
            string strSq = "  select Round(SUM((endtime - starttime) * 24),2)/count(incident) as Hourss  from incidents where  ProcessName  like N'" + drpBusinessUnit.SelectedValue.Trim() + "%' and status=2";
            DataTable dtsq = DataAccess.Instance("BizDB").ExecuteDataTable(strSq.ToString());
            string strAvgDay = null;
            if (dtsq.Rows.Count> 0)
            {
                double Horus = double.Parse(dtsq.Rows[0]["Hourss"].ToString());
                double dAvgDay = Math.Round(Horus, 0, MidpointRounding.AwayFromZero);
                strAvgDay = dAvgDay.ToString();
            }
            return strAvgDay;
        }

        public string GetAvgHours(string strHour)
        {
            string strSq = "  select Round(SUM((endtime - starttime)),2)/count(incident) as Days  from incidents where  ProcessName  like N'" + drpBusinessUnit.SelectedValue.Trim() + "%' and status=2";
            DataTable dtsq = DataAccess.Instance("BizDB").ExecuteDataTable(strSq.ToString());
            string strAvgDay = null;
            if (dtsq.Rows.Count > 0)
            {
                double Days = double.Parse(dtsq.Rows[0]["Days"].ToString());
                double dAvgDay = Math.Round(Days, 0, MidpointRounding.AwayFromZero);
                strAvgDay = dAvgDay.ToString();
            }
            return strAvgDay;
        }
        

        //// 平均消耗（小时）
        //public string GetAvgHours(string strHour)
        //{
        //    string strHorus = null;
        //    //if (EndTime != "")
        //    //{
        //    //    DateTime dtStartTime = DateTime.Parse(StartTime);
        //    //    DateTime dtEndTime = DateTime.Parse(EndTime);
        //    //    TimeSpan tsResult = dtEndTime - dtStartTime;
        //    //    double days = tsResult.TotalHours;
        //    //    strHorus = days.ToString();
        //    //}
        //    if (strHour != "")
        //    {
        //        double dHour = double.Parse(strHour);
        //        double dAvgHour = Math.Round(dHour, 0, MidpointRounding.AwayFromZero);
        //        strHorus = dAvgHour.ToString();
        //    }
        //    return strHorus;
        //}

        private void MessageBox(string script)
        {
            this.Page.ClientScript.RegisterClientScriptBlock(this.Page.GetType(), "javascript", "<script language='javascript' defer>" + script + "</script>");
        }

        protected void btnSearch_Click(object sender, EventArgs e)
        {
            BindData();
            Page.ClientScript.RegisterStartupScript(Page.GetType(), "onload", "document.getElementById('container').style.display='none'", true); 
            
        }
    }
}